//
//  Third.swift
//  mng
//
//  Created by WTFKL on 2018/4/18.
//  Copyright © 2018年 WTF. All rights reserved.
//

import GameplayKit
import SpriteKit

class Third: GameScene {
    private var partition : SKSpriteNode?;
    private var partition2 : SKSpriteNode?;
    private var partition3 : SKSpriteNode?;
    private var partition4 : SKSpriteNode?;
    private var partition5 : SKSpriteNode?;
    private var partition6 : SKSpriteNode?;
    private var partition7 : SKSpriteNode?;
    private var keySprite : SKSpriteNode?;
    private var thornNode : SKSpriteNode?;
    private var thornNode2 : SKSpriteNode?;
    
    
    override func didMove(to view: SKView) {
        super.didMove(to: view);
        initThirdWorld();
    }
    func initThirdWorld(){
        tips?.fontColor = .white;
        tips?.text = "相信我，你的头比你想象的要硬。";
        mesprite?.lightingBitMask = LightBitMaskType.test;
        mesprite?.shadowedBitMask = LightBitMaskType.test;
        ground?.lightingBitMask = LightBitMaskType.test;
        walllf?.lightingBitMask = LightBitMaskType.test;
        wallrt?.lightingBitMask = LightBitMaskType.test;
        doorWall?.lightingBitMask = LightBitMaskType.test;
        
        let light = SKLightNode();
        light.lightColor = UIColor.white;
        light.categoryBitMask = LightBitMaskType.test;
        light.position = CGPoint(x: 0, y: (mesprite?.size.height)!/2);
        light.falloff = 1;
        light.isEnabled = true;
     
        let light2 = SKLightNode();
        light2.lightColor = UIColor.white;
        light2.categoryBitMask = LightBitMaskType.test;
        light2.position = CGPoint(x:  (mesprite?.size.width)!*4, y: (mesprite?.size.height)!/2);
        light2.falloff = 1;
        light2.isEnabled = true;
        
        let light3 = SKLightNode();
        light3.lightColor = UIColor.white;
        light3.categoryBitMask = LightBitMaskType.test;
        light3.position = CGPoint(x:  -(mesprite?.size.width)!*4, y: (mesprite?.size.height)!/2);
        light3.falloff = 1;
        light3.isEnabled = true;
        mesprite?.addChild(light2);
        mesprite?.addChild(light3);
        mesprite?.addChild(light);
        
        
        partition = SKSpriteNode(color: .black, size: CGSize(width: 50, height: 20));
        let partitionX = (walllf?.position.x)! + (walllf?.size.width)!/2 + (partition?.size.width)!/2;
        partition?.position = CGPoint(x: partitionX, y: screenHeight/2/4);
        partition?.physicsBody = SKPhysicsBody(rectangleOf: (partition?.frame.size)!);
        partition?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition?.physicsBody?.affectedByGravity = true;
        partition?.physicsBody?.isDynamic = false;
        partition?.lightingBitMask = LightBitMaskType.test;
        self.addChild(partition!);
        
        partition2 = SKSpriteNode(color: .black, size: CGSize(width: 50, height: 20));
//        let partition2X = (walllf?.position.x)! + (walllf?.size.width)!/2 + (partition?.size.width)!/2;
        partition2?.position = CGPoint(x: 0, y: -screenHeight/2/5);
        partition2?.physicsBody = SKPhysicsBody(rectangleOf: (partition2?.frame.size)!);
        partition2?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition2?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition2?.physicsBody?.affectedByGravity = true;
        partition2?.physicsBody?.isDynamic = false;
        partition2?.lightingBitMask = LightBitMaskType.test;
        self.addChild(partition2!);
        
        
        partition3 = SKSpriteNode(color: .black, size: CGSize(width: 100, height: 20));
        let partition3X = (wallrt?.position.x)! - (wallrt?.size.width)!/2 - (partition3?.size.width)!/2;
        partition3?.position = CGPoint(x: partition3X, y: screenHeight/2/4);
        partition3?.physicsBody = SKPhysicsBody(rectangleOf: (partition3?.frame.size)!);
        partition3?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition3?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition3?.physicsBody?.affectedByGravity = true;
        partition3?.physicsBody?.isDynamic = false;
        partition3?.lightingBitMask = LightBitMaskType.test;
        self.addChild(partition3!);
        
        
        partition4 = SKSpriteNode(color: .black, size: CGSize(width: 20, height: 20));
        let partition4X = ((partition3?.position.x)! - (partition2?.position.x)!)/2
        let partition4Y = (partition3?.position.y)! - ((partition3?.position.y)! - (partition2?.position.y)!)/2
        partition4?.position = CGPoint(x: partition4X, y: partition4Y);
        partition4?.physicsBody = SKPhysicsBody(rectangleOf: (partition4?.frame.size)!);
        partition4?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition4?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition4?.physicsBody?.affectedByGravity = false;
        partition4?.physicsBody?.isDynamic = false;
        partition4?.lightingBitMask = LightBitMaskType.test;
        partition4?.physicsBody?.mass = 3;
        self.addChild(partition4!);
        
        partition5 = SKSpriteNode(color: .black, size: CGSize(width: 10, height: 100));
        partition5?.anchorPoint = CGPoint(x: 0.5, y: 0);
        partition5?.position = CGPoint(x: 0, y: screenHeight/2-(partition5?.size.height)!);
        partition5?.physicsBody = SKPhysicsBody(rectangleOf: (partition5?.size)!, center:CGPoint(x: 0, y:  (partition5?.size.height)!/2));
        partition5?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition5?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition5?.physicsBody?.collisionBitMask = BitMaskType.charactor;
        partition5?.physicsBody?.affectedByGravity = false;
        partition5?.physicsBody?.isDynamic = false;
        partition5?.lightingBitMask = LightBitMaskType.test;
        partition5?.physicsBody?.mass = 3;
        self.addChild(partition5!);
        
        
        partition6 = SKSpriteNode(color: .black, size: CGSize(width: 150, height: 10));
        partition6?.position = (partition5?.position)!;
        partition6?.physicsBody = SKPhysicsBody(rectangleOf: (partition6?.size)!);
        partition6?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition6?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition6?.physicsBody?.collisionBitMask = BitMaskType.charactor;
        partition6?.physicsBody?.affectedByGravity = false;
        partition6?.physicsBody?.isDynamic = true;
        partition6?.lightingBitMask = LightBitMaskType.test;
        partition6?.physicsBody?.mass = 1;
        self.addChild(partition6!);
        
        
        let partition7X = (partition?.position.x)!/2-20;
        partition7 = SKSpriteNode(color: .black, size: CGSize(width: 30, height: 10));
        partition7?.position = CGPoint(x: partition7X, y: (partition?.position.y)!+20);
        partition7?.physicsBody = SKPhysicsBody(rectangleOf: (partition6?.size)!);
        partition7?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition7?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition7?.physicsBody?.collisionBitMask = BitMaskType.charactor;
        partition7?.physicsBody?.affectedByGravity = false;
        partition7?.physicsBody?.isDynamic = false;
        partition7?.lightingBitMask = LightBitMaskType.test;
        partition7?.physicsBody?.mass = 1;
        self.addChild(partition7!);
        
        let phyContact = SKPhysicsJointPin.joint(withBodyA: (partition5?.physicsBody)!, bodyB: (partition6?.physicsBody)!, anchor:(partition6?.position)!);
        self.physicsWorld.add(phyContact);
        
        keySprite = MeKey(mName: "key", posistion: CGPoint(x: (partition?.position.x)!, y: (partition?.position.y)!+40));
        self.addChild(keySprite!);
        let keylight = SKLightNode();
//        keylight.lightColor = UIColor.white;
        keylight.categoryBitMask = LightBitMaskType.test;
        keylight.position = CGPoint(x: 0, y: 0);
        keylight.falloff = 0;
        keylight.ambientColor = .white;
        keylight.isEnabled = true;
        keySprite?.addChild(keylight);
        
        thornNode = SKSpriteNode(texture: SKTexture(image: #imageLiteral(resourceName: "thorn")));
        thornNode?.size = CGSize(width: 50, height: 25);
        thornNode?.position = (doorWall?.position)!;
        thornNode?.lightingBitMask = LightBitMaskType.test;
        thornNode?.zPosition = 1;
        thornNode?.physicsBody = SKPhysicsBody(texture: (thornNode?.texture)!, size: (thornNode?.size)!);
        thornNode?.physicsBody?.categoryBitMask = BitMaskType.damageThing;
        thornNode?.physicsBody?.collisionBitMask = BitMaskType.charactor;
        thornNode?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        thornNode?.physicsBody?.isDynamic = false;
        thornNode?.physicsBody?.affectedByGravity = false;
        
    
        let firelight = SKLightNode();
        firelight.lightColor = UIColor.white;
        firelight.categoryBitMask = LightBitMaskType.test;
        firelight.position = CGPoint(x: 0, y: 0);
        firelight.falloff = 1;
        firelight.isEnabled = true;
        
        let fire = SKEmitterNode(fileNamed: "FireParticle");
       
        let rotateAngle = fire?.emissionAngle;
        thornNode2 = SKSpriteNode(color: .clear, size: CGSize(width: 70, height: 35));
        thornNode2?.physicsBody = SKPhysicsBody(rectangleOf: (thornNode2?.size)!);
        thornNode2?.physicsBody?.categoryBitMask = BitMaskType.damageThing;
        thornNode2?.physicsBody?.collisionBitMask = BitMaskType.charactor;
        thornNode2?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        thornNode2?.physicsBody?.isDynamic = false;
        thornNode2?.physicsBody?.affectedByGravity = false;
        
        let thornY = screenHeight/2-(screenHeight/2 - (partition3?.position.y)!)/1.5;
        thornNode2?.position = CGPoint(x: 0, y: thornY);
        thornNode2?.addChild(fire!);
        thornNode2?.addChild(firelight);
        
        
        self.addChild(thornNode2!);
        let mm = SKAction.moveTo(x: -screenWidth/2, duration: 3);
        let mm2 = SKAction.moveTo(x: screenWidth/2, duration: 3);
        let Firemm = SKAction.run {
            fire?.emissionAngle = 0;
        };
        let Firemm2 = SKAction.run {
            fire?.emissionAngle = rotateAngle!;
        };
        let toto = SKAction.sequence([Firemm,mm,Firemm2,mm2]);
        let repet = SKAction.repeatForever(toto);
        thornNode2?.run(repet);
    
        let chufaqi1 = SK_Trigger(mName: "trigger1",
                                  mSize: CGSize.init(width: 20, height: 20),
                                  mColor: .clear,
                                  mPosition: CGPoint.init(x: -100, y: (ground?.position.y)!+(ground?.size.height)!/2+10),
                                  mCollisonBitMask: BitMaskType.charactor,
                                  mContactTestBitMask: BitMaskType.charactor);
        self.addChild(chufaqi1);
    }
    
    
    
    override func didBegin(_ contact: SKPhysicsContact) {
        super.didBegin(contact);
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.other) {//人物与触发器
                contact.bodyB.node?.removeFromParent();
                self.addChild(thornNode!);
                let action = SKAction.moveTo(x: -screenWidth/2, duration: 1);
                let action2 = SKAction.removeFromParent();
                let seque = SKAction.sequence([action,action2]);
                thornNode?.run(seque);
        }
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.damageThing) {//陷阱与角色
            let charactor = contact.bodyA.node as? SKSpriteNode;
            let fallball = contact.bodyB.node as? SKSpriteNode;
            mesprite?.isAlive = false;
            charactor?.removeFromParent();
            fallball?.removeFromParent();
        }
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.key) {//人物与钥匙
            let mykeyNode = contact.bodyB.node as? SKSpriteNode;
            mesprite?.isGetKey = true;
            mykeyNode?.removeFromParent();
        }
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.other) {//人物与过关触发器
            let successNode = contact.bodyB.node as? SKSpriteNode;
            if(successNode?.name == "success"){
                self.view?.presentScene(SKScene(fileNamed: "Fourth")!,transition: SKTransition.crossFade(withDuration: 0.5));
            }
        }
    }
    
    override func update(_ currentTime: TimeInterval) {
        super.update(currentTime);
        if mesprite?.isAlive == false {
            self.view?.presentScene(SKScene(fileNamed: "Third")!,transition: SKTransition.crossFade(withDuration: 1));
            print("------die");
        }
    }
}
